<?php
// +----------------------------------------------------------------------
// | CSMITSM  [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2024 https://www.fastadmin.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: chenshiming
// +----------------------------------------------------------------------

namespace app\admin\controller\csmitsm;

use addons\csmitsm\library\xcore\xcore\base\XcABackend;
use addons\csmitsm\library\xcore\xcore\utils\XcDaoUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcAssertUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcRequestUtils;
use addons\csmitsm\library\xcore\xcflow\utils\XcformFieldsUtils;
use addons\csmitsm\library\xcore\xcflow\utils\XcformXcchartUtils;

class XcflowviewpageviewInstance extends XcABackend
{


    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\csmitsm\Xcflowchartpage();
    }

    // http://fastadmin_addons_csmitsm.fa.net/abc.php/csmitsm/xcflowviewpageview_instance/detail?xcflowchartpage_id=1
    public function detail()
    {
        $xcflowchartpage_id = XcRequestUtils::get("xcflowchartpage_id");
        $xcflowchartpage = XcDaoUtils::getRowById($this->model, $xcflowchartpage_id);
        $xccontrol = "xcchart";

        $xcfieldjsonJsonObject = XcformFieldsUtils::getSimpleXcfieldjsonJsonObjectFromXcform($xcflowchartpage);
        $xcfieldtags = XcformFieldsUtils::getAllXcfieldtags($xccontrol);

        $this->assignconfig("xcform", $xcflowchartpage);
        $this->assignconfig("fieldfulljson", $xcfieldjsonJsonObject);
        $this->view->assign("xcfieldtags", $xcfieldtags);
        $this->view->assign("xccontrol", $xccontrol);
        $this->assignconfig("controlPath", $this->getControlPath());
        $this->view->assign("include_files", ["echarts"]);
        return $this->fetch();
    }
    
 
    public function getXcchartOptions()
    {
        $xcform_id = XcRequestUtils::get("xcform_id");
        $__vModel__ = XcRequestUtils::get("__vModel__");

        $targetFieldjson = null;
        if (true) {
            $xcflowchartpage = XcDaoUtils::getRowById($this->model, $xcform_id);
            $xcfieldjsonJsonObject = XcformFieldsUtils::getSimpleXcfieldjsonJsonObjectFromXcform($xcflowchartpage);
            foreach ($xcfieldjsonJsonObject as $item) {
                if ($item['__vModel__'] == $__vModel__) {
                    $targetFieldjson = $item;
                    break;
                }
            }
            XcAssertUtils::assertNotNull($targetFieldjson);
        }

        [$dimlabels, $facts] = XcformXcchartUtils::getXcchartOptions($targetFieldjson['fieldjsonObj']['xcchart_data_dim'], $targetFieldjson['fieldjsonObj']['xcchart_data_facts']);

        $this->success(null, null, [
            'dimlables' => $dimlabels,
            'facts' => $facts
        ]);
    }
}
